<template>
  <div class="merchants-page">
    <!-- 页面头部 -->
    <section class="page-header">
      <div class="container">
        <h1 class="title-primary">优质商家</h1>
        <p class="subtitle">发现更多优质货源商家，开启合作之旅</p>
      </div>
    </section>

    <!-- 筛选和搜索 -->
    <section class="filters">
      <div class="container">
        <div class="filter-bar">
          <div class="search-box">
            <el-input
              v-model="searchQuery"
              placeholder="搜索商家名称或描述"
              size="large"
              clearable
            >
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </el-input>
          </div>
          <div class="category-filter">
            <el-select
              v-model="selectedCategory"
              placeholder="选择分类"
              size="large"
              clearable
            >
              <el-option label="全部分类" value="" />
              <el-option
                v-for="category in categories"
                :key="category"
                :label="category"
                :value="category"
              />
            </el-select>
          </div>
          <div class="sort-filter">
            <el-select
              v-model="sortBy"
              placeholder="排序方式"
              size="large"
            >
              <el-option label="评分最高" value="rating" />
              <el-option label="成立时间" value="established" />
              <el-option label="名称排序" value="name" />
            </el-select>
          </div>
        </div>
      </div>
    </section>

    <!-- 商家列表 -->
    <section class="merchants-list section">
      <div class="container">
        <div class="merchants-grid">
          <div
            v-for="merchant in filteredMerchants"
            :key="merchant.id"
            class="merchant-card card"
            @click="$router.push(`/merchant/${merchant.id}`)"
          >
            <div class="merchant-image">
              <img :src="merchant.image" :alt="merchant.name" />
              <div class="merchant-category">{{ merchant.category }}</div>
              <div v-if="merchant.featured" class="featured-badge">精选</div>
            </div>
            <div class="merchant-info">
              <h3>{{ merchant.name }}</h3>
              <p>{{ merchant.description }}</p>
              <div class="merchant-details">
                <div class="detail-item">
                  <el-icon><Location /></el-icon>
                  <span>{{ merchant.location }}</span>
                </div>
                <div class="detail-item">
                  <el-icon><Calendar /></el-icon>
                  <span>{{ merchant.established }}年成立</span>
                </div>
                <div class="detail-item">
                  <el-icon><Star /></el-icon>
                  <span>{{ merchant.rating }}分</span>
                </div>
              </div>
              <div class="merchant-specialties">
                <el-tag
                  v-for="specialty in merchant.specialties"
                  :key="specialty"
                  size="small"
                  type="info"
                >
                  {{ specialty }}
                </el-tag>
              </div>
              <div class="merchant-footer">
                <span class="price-range">{{ merchant.priceRange }}</span>
                <span class="min-order">{{ merchant.minOrder }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 空状态 -->
        <div v-if="filteredMerchants.length === 0" class="empty-state">
          <el-empty description="暂无符合条件的商家" />
        </div>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { Search, Location, Calendar, Star } from '@element-plus/icons-vue'
import merchantsData from '../data/merchants.json'

interface Merchant {
  id: number
  name: string
  category: string
  description: string
  image: string
  rating: number
  location: string
  established: string
  specialties: string[]
  minOrder: string
  priceRange: string
  featured: boolean
}

const merchants = ref<Merchant[]>([])
const searchQuery = ref('')
const selectedCategory = ref('')
const sortBy = ref('rating')

// 获取所有分类
const categories = computed(() => {
  const cats = merchants.value.map(m => m.category)
  return [...new Set(cats)]
})

// 筛选和排序商家
const filteredMerchants = computed(() => {
  let result = merchants.value

  // 搜索筛选
  if (searchQuery.value) {
    const query = searchQuery.value.toLowerCase()
    result = result.filter(merchant =>
      merchant.name.toLowerCase().includes(query) ||
      merchant.description.toLowerCase().includes(query)
    )
  }

  // 分类筛选
  if (selectedCategory.value) {
    result = result.filter(merchant => merchant.category === selectedCategory.value)
  }

  // 排序
  result.sort((a, b) => {
    switch (sortBy.value) {
      case 'rating':
        return b.rating - a.rating
      case 'established':
        return parseInt(b.established) - parseInt(a.established)
      case 'name':
        return a.name.localeCompare(b.name)
      default:
        return 0
    }
  })

  return result
})

onMounted(() => {
  merchants.value = merchantsData as Merchant[]
})
</script>

<style scoped>
.merchants-page {
  min-height: 100vh;
  background: var(--accent-color);
}

/* 页面头部 */
.page-header {
  background: white;
  padding: 80px 0 60px;
  text-align: center;
}

/* 筛选栏 */
.filters {
  background: white;
  padding: 0 0 40px;
  border-bottom: 1px solid var(--border-color);
}

.filter-bar {
  display: flex;
  gap: 20px;
  align-items: center;
  flex-wrap: wrap;
}

.search-box {
  flex: 1;
  min-width: 300px;
}

.category-filter,
.sort-filter {
  min-width: 150px;
}

/* 商家列表 */
.merchants-list {
  padding: 60px 0;
}

.merchants-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 30px;
}

.merchant-card {
  cursor: pointer;
  transition: all 0.3s ease;
  background: white;
}

.merchant-card:hover {
  transform: translateY(-6px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
}

.merchant-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.merchant-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.merchant-card:hover .merchant-image img {
  transform: scale(1.05);
}

.merchant-category {
  position: absolute;
  top: 15px;
  right: 15px;
  background: var(--primary-color);
  color: white;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 0.875rem;
  font-weight: 500;
}

.featured-badge {
  position: absolute;
  top: 15px;
  left: 15px;
  background: #f39c12;
  color: white;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 0.875rem;
  font-weight: 500;
}

.merchant-info {
  padding: 25px;
}

.merchant-info h3 {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 10px;
  color: var(--text-primary);
}

.merchant-info p {
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 20px;
}

.merchant-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 15px;
}

.detail-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 0.875rem;
}

.merchant-specialties {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.merchant-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 15px;
  border-top: 1px solid var(--border-color);
}

.price-range {
  font-weight: 600;
  color: var(--primary-color);
}

.min-order {
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.empty-state {
  text-align: center;
  padding: 60px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-box {
    min-width: auto;
  }
  
  .merchants-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
}
</style>
